package cn.e4j.modular.api.service;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.IDto;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat310Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class Gwat310Service {
    @Autowired
    Gwat310Mapper gwat310Mapper;

    @Autowired
    GwaService gwaService;


    /**
     * 获取订单信息
     *
     * @param inDto
     * @return
     */
    public List<Dto> getGwcsData(Page<Object> page,Dto inDto) {
        inDto.put("GWCSENT", inDto.getString("ENT"));
        inDto.put("LANG", "2");
        List<Dto> dtoList = gwat310Mapper.getGwcsPage(page,inDto);
        for (Dto dto : dtoList) {
            inDto.put("GWCSDOCNO", dto.getString("GWCSDOCNO"));
            inDto.put("GWCSDOCDATE", dto.getDate("GWCSDOCDATE"));
            List<Dto> list = gwat310Mapper.getGwcsDetail(dto);
            dto.put("list", list);
            BigDecimal amount = new BigDecimal(0);
            for (Dto listDto : list) {
                amount = amount.add(ToolUtil.isEmpty(listDto.getString("GWCS005")) ? new BigDecimal(0) : listDto.getBigDecimal("GWCS005"));
                listDto.put("LANG", "2");
                List<Dto> characteristics = gwat310Mapper.getCharacteristics(listDto);
                listDto.put("CHARACTERISTICS", characteristics);
            }
            dto.put("GWCSAMOUNT", amount);
        }
        return dtoList;
    }

    /**
     * 确认订单--批量
     *
     * @param
     */
//    @Transactional
//    public void confirmBatch(List<Dto> dtoList) {
//        for(Dto inDto : dtoList){
//            confirm(inDto);
//        }
//    }
    /**
     * 确认订单
     *
     * @param
     */
    @Transactional
    public Dto confirm(SysUser userModel,Dto inDto) {
        Dto outDto = Dtos.newDto();
        List<Dto> listDto = new ArrayList<Dto>();
        if ("single".equals(inDto.getString("type"))) {
            listDto = CommonUtils.toJavaList(inDto.getString("list"),Dto.class);
        } else {
            listDto = (List<Dto>)inDto.getList("list");
        }
        for (Dto dto : listDto) {
            dto.put("GWCS014", userModel.getID());
            dto.put("GWCS015", userModel.getMOBILE());
            dto.put("GWCS016", DateUtils.parseDate(dto.getString("GWCS016")));
            gwat310Mapper.confirm(dto);
        }
        outDto.put("GWCSDOCNO", inDto.getString("GWCSDOCNO"));
        return outDto;
    }
    /**
     * 取消确认订单--单笔
     *
     * @param
     */
    @Transactional
    public void reconfirmSingle(Dto inDto) {
        reconfirm(inDto);
    }

    /**
     * 取消确认订单--批量
     *
     * @param
     */
    @Transactional
    public void reconfirmBatch(List<Dto> dtoList) {
        for(Dto inDto : dtoList){
            reconfirm(inDto);
        }
    }

    /**
     * 取消确认订单
     *
     * @param
     */
    @Transactional
    public Dto reconfirm(Dto inDto) {
        inDto.put("GWCSENT", inDto.getString("ENT"));
        Dto outDto = Dtos.newDto();
        gwat310Mapper.reconfirm(inDto);
        outDto.put("GWCSDOCNO", inDto.getString("GWCSDOCNO"));

        return outDto;
    }

    /**
     * 作废订单--单笔
     * @param inDto
     * @return
     */
    @Transactional
    public void deleteSingle(Dto inDto) {
        delete(inDto);
    }

    /**
     * 作废订单--批量
     * @param dtoList
     * @return
     */
    @Transactional
    public ResponseData deleteBatch(Dto indto,List<Dto> dtoList) {
        String ent = indto.getString("ENT");
        for (Dto item : dtoList) {
            item.put("ENT",ent);
            item.put("GWCSENT",ent);
            String result = check(item);
            if (ToolUtil.isEmpty(result)) {
                delete(item);
            }else
                return new ErrorResponseData(500,result);
        }

        return new SuccessResponseData();
    }

    /**
     * 作废订单
     * @param dto
     * @return
     */
    @Transactional
    public Dto delete(Dto dto) {
        Dto outDto = Dtos.newDto();
        dto.put("GWCSENT", dto.getString("GWCSENT"));
        gwat310Mapper.deleteGwcs(dto);
        outDto.put("GWCSDOCNO", dto.getString("GWCSDOCNO"));

        return outDto;
    }

    /**
     * 检查是否可取消确认或作废
     *
     * @param inDto
     */
    public String check(Dto inDto) {
        List<Dto> checklist = gwat310Mapper.getGwc(inDto);
        if (checklist.size() > 0) {
            return inDto.getString("GWCSDOCNO") + "有后续单据,不可操作";
        }
        return null;
    }

    /**
     * 检查是否可确认
     *
     * @param inDto
     */
    public String validate(Dto inDto) {
        List<GwcsT> listDto = new ArrayList<GwcsT>();
        if ("single".equals(inDto.getString("type"))) {
            listDto = CommonUtils.toJavaList(inDto.getString("list"),GwcsT.class);
        } else {
            listDto = (List<GwcsT>)inDto.getList("list");
        }
        for (int i = 0; i < listDto.size(); i++) {
            GwcsT gwcsT = listDto.get(i);
            if (ToolUtil.isEmpty(gwcsT.getGWCS104())) {
                return "单号" + gwcsT.getGWCSDOCNO() + " NO." + (i + 1) + "合同编号为空，请检查";
            } else if (ToolUtil.isEmpty(gwcsT.getGWCS019())) {
                return "单号" + gwcsT.getGWCSDOCNO() + " NO." + (i + 1) + "序列号为空，请检查";
            } else if (ToolUtil.isEmpty(gwcsT.getGWCS016())) {
                return "单号" + gwcsT.getGWCSDOCNO() + " NO." + (i + 1) + "投入使用日期为空，请检查";
            }
        }
        return null;
    }

     /*
     * 获取经销商负责人,线人
     *
     * @param inDto
     * @return
     *//*
    public List<Dto> getGwaa(Dto inDto) {
        inDto.put("gwaaent", "80");
        return sqlDao.list(Gwat310Service.class.getName() + ".getGwaa", inDto);
    }*/
}
